package com.ls.kdlss.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ls.kdlss.R;
import com.ls.kdlss.entity.AddressEntity;
import com.ls.kdlss.ui.Login.SetInfomationActivity;
import com.ls.kdlss.util.HttpStorage;
import com.wx.android.common.util.ActivityUtil;
import com.wx.android.common.util.JsonUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.w3c.dom.Text;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/12.
 */

public class DiZhiActivity extends AppCompatActivity {

    public static void launch(Context context) {

        ActivityUtil.startActivity(context, DiZhiActivity.class);
    }
    public CommonAdapter<AddressEntity.DataBean.SchoolBean> commonAdapter;
    @Bind(R.id.rv_view)
    RecyclerView rvView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dizhi);
        ButterKnife.bind(this);
        setDizhi();
    }


    public void setDizhi() {


        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder().url(HttpStorage.addressUrls()).build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
               final String body=response.body().string();


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        AddressEntity addresentity= JsonUtils.jsonToObject(body, AddressEntity.class);
                        final LinearLayoutManager manager=new LinearLayoutManager(DiZhiActivity.this);
                        rvView.setLayoutManager(manager);

                        commonAdapter=new CommonAdapter<AddressEntity.DataBean.SchoolBean>(DiZhiActivity.this,R.layout.cell_dihzhi, addresentity.getData().getSchool()) {
                            @Override
                            protected void convert(final ViewHolder holder, final AddressEntity.DataBean.SchoolBean s, final int position) {

                                holder.setText(R.id.tv_dizhi,s.getRegion_name());
                                holder.getView(R.id.ll_address).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        TextView textView= holder.getView(R.id.tv_dizhi);
                                        SetInfomationActivity.address=textView.getText().toString();
                                        SetInfomationActivity.addressid=s.getRegion_id();
                                        finish();
                                    }
                                });

                            }
                        };
                        rvView.setAdapter(commonAdapter);
                    }
                });

            }
        });




    }

}
